package com.vizhuo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 配置文件工具类
 * 
 * @author YuanWenLong 作者 E-mail: yuanwenlong2010@163.com
 * @date 创建时间：2015年6月25日 下午7:47:02
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class PropertiesUtil {

    public static final String CONFIG_PATH = "/config/properties/config.properties";

    public static final String PUSH_PATH = "/config/properties/push.properties";

    public static final String MEMCACHED_PATH = "/config/properties/memcached.properties";

    public static final String CONFIG_UPLOAD_APP_PATH = "upload.app.path";

    public static final String CONFIG_UPLOAD_APP_URL = "upload.app.url";

    public static final String CONFIG_LOCALHOST_PATH = "localhost.path";

    public static final String CONFIG_KINDEDITOR_UPLOAD_PATH = "kindeditor.upload.path";

    public static final String CONFIG_KINDEDITOR_UPLOAD_URL = "kindeditor.upload.url";

    public static final String CONFIG_LOG_INFO_PATH = "log.into.path";

    public static final String IOS_DRIVER_PLIST = "ios.driver.plist";

    public static final String IOS_CONSIGNER_PLIST = "ios.consigner.plist";

    public static final String IOS_INFO_PLIST = "ios.info.plist";

    public static final String IOS_INFO_TRANSITION_PLIST = "ios.info.transition.plist";

    public static final String CONFIG_INFORMATION_PIC_PATH = "informationPicPath";

    public static final String CONFIG_PROMOTION_PIC_PATH = "promotionPicPath";

    public static final String CONFIG_WAREHOUSE_PIC_PATH = "warehousePicPath";

    public static final String CONFIG_ID_MANAGER_STEP_SIZE = "idManagerStepSize";

    public static final String MEMCACHED_PATH_MEMCACHEDFlAG = "memcachedFlag";

    /**
     * 同城的最大直线距离，货运距离大于该值会推送同城&干线
     */
    public static final String SAME_CITY_MAX_DISTANCE = "sameCity.maxDistance";

    /**
     * 同城推送范围,单位:公里,等同于给货源起点100公里范围内的司机推送
     */
    public static final String PUSH_CITY_SCOPE = "push.city.scope";

    /**
     * 同城推送范围,单位:公里,等同于给货源起点100公里范围内的司机推送
     */
    public static final String FIND_BOUND_SCOPE = "find.bound.scope";

    /**
     * 干线的最小直线距离，货运距离小于该值会推送同城&干线
     */
    public static final String DIFFERENCE_CITY_MIN_DISTANCE = "differenceCity.minDistance";

    /**
     * nfs根目录访问url，使用时要和CONFIG_LOCALHOST_PATH拼接在一起。
     */
    public static final String CONFIG_NFS_URL = "nfs_url";

    /**
     * nfs根目录。
     */
    public static final String CONFIG_NFS_PATH = "nfs_path";

    /**
     * 发货人，图片存放路径
     */
    public static final String CONFIG_MEB_CONSIGNER_PIC_PATH = "mebConsignerPicPath";

    /**
     * 司机，图片存放路径
     */
    public static final String CONFIG_MEB_DRIVER_PIC_PATH = "mebDriverPicPath";

    /**
     * 信息部等，图片存放路径
     */
    public static final String CONFIG_MEB_INFO_PIC_PATH = "mebInfoPicPath";

    /**
     * 呼叫中心服务器IP
     */
    public static final String CALL_CENTER_SERVER_IP = "call_center_server_ip";

    /**
     * 身份证认证权限开关
     */
    public static final String IDCARD_CHECK_SWITCH = "idcard_check_switch";

    public static String readValue(String propertiesPath, String key) {
        Properties props = new Properties();
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        File file = new File(path);
        while (!file.getName().toUpperCase().equals("CLASSES")) {
            file = file.getParentFile();
        }
        path = file.getAbsolutePath().replaceAll("\\\\", "/");
        path += propertiesPath;
        FileInputStream fis = null;
        try {

            fis = new FileInputStream(path);
            // 不能用以下的方式，否则必须要重启服务器才能读取到最新的数据，问题就出在这
            // InputStream in =
            // Thread.currentThread().getContextClassLoader().getResourceAsStream(isParamProper?paramsPath:counterPath);
            // props.load(in);
            props.load(fis);
            String value = props.getProperty(key);
            return value.trim();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
